﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 知识文档
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017-12-08 
    /// </summary>
    public class DocumentBll
    {
        private DocumentIService service = new DocumentService();

        /// <summary>
        /// 列表（全部文档）
        /// </summary>
        /// <returns></returns>
        public async Task<List<DocumentEntity>> GetFolderList()
        {
            try
            {
                var userId = UserProvider.Instance.Get().UserId;
                return await service.GetFolderList(userId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表（全部文档）
        /// </summary>
        /// <param name="parentId">文档父级</param>
        /// <returns></returns>
        public async Task<List<DocumentEntity>> GetAllList(string parentId)
        {
            try
            {
                var userId = UserProvider.Instance.Get().UserId;
                return await service.GetAllList(userId, parentId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表（回收站）
        /// </summary>
        /// <returns></returns>
        public async Task<List<DocumentEntity>> GetTrashList()
        {
            try
            {
                var userId = UserProvider.Instance.Get().UserId;
                return await service.GetTrashList(userId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表（我的共享）
        /// </summary>
        /// <returns></returns>
        public async Task<List<DocumentEntity>> GetShareOutList()
        {
            try
            {
                var userId = UserProvider.Instance.Get().UserId;
                return await service.GetShareOutList(userId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表（共享给我）
        /// </summary>
        /// <returns></returns>
        public async Task<List<DocumentEntity>> GetShareTomeList()
        {
            try
            {
                var userId = UserProvider.Instance.Get().UserId;
                return await service.GetShareTomeList(userId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表（共享人员）
        /// </summary>
        /// <param name="documentId">文档主键</param>
        /// <returns></returns>
        public async Task<List<DocumentShareEntity>> GetShareUserList(string documentId)
        {
            try
            {
                return await service.GetShareUserList(documentId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<DocumentEntity> GetInfo(string id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task<int> Delete(DocumentEntity entity)
        {
            try
            {
                entity.DeleteMark = 1;
                entity.DeleteTime = DateTime.Now;
                entity.DeleteUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(DocumentEntity entity)
        {
            try
            {
                entity.Id = CommonHelper.Guid;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                entity.DeleteMark = 0;
                return await service.Create(entity);
            }
            catch (Exception)
            {
                return -1;
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        public async Task<int> Update(string id, DocumentEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 共享文件（创建）
        /// </summary>
        /// <param name="documentId">文档主键</param>
        /// <param name="shareUserId">共享用户</param>
        public async Task ShareCreate(string documentId, string[] shareUserId)
        {
            try
            {
                List<DocumentShareEntity> documentShareEntityList = new List<DocumentShareEntity>();
                foreach (var item in shareUserId)
                {
                    documentShareEntityList.Add(new DocumentShareEntity
                    {
                        Id = CommonHelper.Guid,
                        DocumentId = documentId,
                        ShareUserId = item,
                        ShareTime = DateTime.Now,
                    });
                }
                await service.ShareCreate(documentId, documentShareEntityList);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 共享文件（取消）
        /// </summary>
        /// <param name="documentId">文档主键</param>
        public async Task ShareCancel(string documentId)
        {
            try
            {
                await service.ShareCancel(documentId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 回收站（删除）
        /// </summary>
        /// <param name="id">主键值</param>
        public async Task TrashDelete(string id)
        {
            try
            {
                var list = await service.GetChildList(id);
                foreach (DocumentEntity entity in list)
                {
                    //删除文件
                    FileHelper.DeleteFile(ConfigurationKey.DocumentFilePath + entity.FilePath);
                }
                await service.Delete(list);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 回收站（还原）
        /// </summary>
        /// <param name="id">主键值</param>
        public async Task TrashRecovery(string id)
        {
            try
            {
                var entity = await this.GetInfo(id);
                entity.DeleteMark = 0;
                entity.DeleteTime = null;
                entity.DeleteUserId = null;
                await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 文件/夹移动到
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="toId">将要移动到Id</param>
        public async Task MoveTo(string id, string toId)
        {
            DocumentEntity entity = await this.GetInfo(id);
            entity.ParentId = toId;
            await service.Update(entity);
        }
        /// <summary>
        /// 重复验证（名称）
        /// </summary>
        /// <param name="fullName">名称</param>
        /// <param name="id">键值</param>
        /// <returns></returns>
        public async Task<bool> IsExistByFullName(string fullName, string id)
        {
            try
            {
                var whereLambda = ExtLinq.True<DocumentEntity>();
                whereLambda = whereLambda.And(m => m.FullName == fullName);
                if (!string.IsNullOrEmpty(id))
                    whereLambda = whereLambda.And(m => m.Id != id);
                return await service.IsExist(whereLambda);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }

        }
    }
}